<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=UTF-8");
%>
<html>
<head>
<META http-equiv=Content-Type content="text/html; charset=UTF-8">
<!--LINK href="images/css.css" type=text/css rel=stylesheet-->
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</head>
<%
    String driver = "oracle.jdbc.driver.OracleDriver";
    String connectionURL = "jdbc:oracle:thin:@localhost:1521:XE"; 
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;
    List pollid = new ArrayList();

    try{
        Class.forName(driver); 
    } catch(java.lang.ClassNotFoundException e) {
        e.printStackTrace();
    }
    try {
        conn = DriverManager.getConnection(connectionURL,"2","2"); 
        st=conn.createStatement();
        rs=st.executeQuery("SELECT distinct Poll_Question.PollID FROM Poll_Question order by PollID asc");
        while (rs.next())
        {
            pollid.add(rs.getInt(1));
        }
        rs.close();
        st.close();
        conn.close();
    }  catch (Exception e)  {   
        e.printStackTrace();
    }
%>
<body>
<p>
  <script language="javascript">
	var XMLHttpReq;
    var currentSort;
    function createXMLHttpRequest() {
        if(window.XMLHttpRequest) { 
            XMLHttpReq = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { 
            try {
                XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
    }
    
    function sendRequest(url) {
        createXMLHttpRequest();
        XMLHttpReq.open("GET", url, true);
        XMLHttpReq.onreadystatechange = processResponse;
        XMLHttpReq.send(null);  
    }

    function processResponse() {
        if (XMLHttpReq.readyState == 4) {
            if (XMLHttpReq.status == 200) { 
                updateMenu();
            } else { 
                  alert("Request failed.");
            }
        }
    }

    function updateMenu() {
        var res=XMLHttpReq.responseXML.getElementsByTagName("res")
        var questionidrecord = document.all.questionidrecord;
        questionidrecord.options.length=0;
        questionidrecord.add(new Option("Version",""));
        for(var i=0;i<res.length;i++){
        	questionidrecord.add(new Option(res[i].firstChild.data,res[i].firstChild.data));
        }
    }

    function showSubMenu(obj) {
        sendRequest("pollv.jsp?sort=" + obj);
    }

    function sendRedirect(){
		var pid=pollidrecord.options[pollidrecord.selectedIndex].text;
		var pv=questionidrecord.options[questionidrecord.selectedIndex].text;
        if(pid=="PollID"){
            alert("Please select a poll ID")
            return;
            }
        if(pv=="Version"){
            alert("Please select a Version")
            return;
            }
</script>
 
&nbsp;&nbsp;&nbsp;<span class="STYLE2">Poll ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Version</span></p>
<p>
  <select onChange="showSubMenu(this.options[this.options.selectedIndex].value)" name="pollidrecord"  style="width:80px">
    <option value=''>-PollID-</option>
    <%
    for(int i=0;i<pollid.size();i++)
    {
        out.println("<option value='"+pollid.get(i)+"'>"+pollid.get(i)+"</option>");
    }
%>
  </select>
  <select name="questionidrecord" style="width:100px">
    <option name="">Version</option>
  </select>
</p>
</body>
</html>